package utils

import (
	"context"
	log "github.com/sirupsen/logrus"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
)

func MgoList(ctx context.Context, col *mongo.Collection, query bson.M, result interface{}) error {
	res, err := col.Find(ctx, query)
	if err != nil {
		log.Errorf("查询数据失败，query:%v,err:%v", query, err)
		return err
	}
	err = res.All(ctx, result)
	if err != nil {
		log.Errorf("序列化数据失败，query:%v,err:%v", query, err)
		return err
	}
	return nil
}

func MgoGet(ctx context.Context, col *mongo.Collection, query bson.M, result interface{}) error {
	one := col.FindOne(ctx, query)

	if one.Err() != nil {
		log.Errorf("查询数据失败，query:%v,err:%v", query, one.Err())
		return one.Err()
	}

	err := one.Decode(result)
	if err != nil {
		log.Errorf("序列化数据失败，query:%v,err:%v", query, err)
		return err
	}
	return nil
}
